package com.atguigu.gmall.product.rpc;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.product.SkuImage;
import com.atguigu.gmall.model.product.SkuInfo;
import com.atguigu.gmall.model.product.SkuSaleAttrValue;
import com.atguigu.gmall.model.product.SpuSaleAttr;
import com.atguigu.gmall.product.service.SkuImageService;
import com.atguigu.gmall.product.service.SkuInfoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * @Author 卡尼奶
 * @Date 2022/5/22 18:49
 * @Version 1.0
 */
@RestController
@RequestMapping("/rpc/inner/product")
public class SkuInfoRpcController {

    @Autowired
    private SkuInfoService skuInfoService;

    @Autowired
    private SkuImageService skuImageService;

    /**
     * 根据skuId 查询Sku基本信息
     * @param skuId
     * @return
     */
    @GetMapping("/getSkuInfo/{skuId}")
    public Result<SkuInfo> getSkuInfo(@PathVariable("skuId")Long skuId){
        SkuInfo skuInfo = skuInfoService.getById(skuId);
        QueryWrapper<SkuImage> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("sku_id",skuId);
        List<SkuImage> list = skuImageService.list(queryWrapper);
        skuInfo.setSkuImageList(list);
        return Result.ok(skuInfo);
    }

    /**
     * 查询商品价格
     * @param skuId
     * @return
     */
    @GetMapping("/skuinfo/price/{skuId}")
    public Result<BigDecimal> getPrice(@PathVariable("skuId")Long skuId){
        BigDecimal price =  skuInfoService.getPrice(skuId);
        return Result.ok(price);
    }

    /**
     * 根据skuId查询所有销售属性和值
     * @param skuId
     * @return
     */
    @GetMapping("/getSkuSaleAttrAndValue/{skuId}")
    public Result<List<SpuSaleAttr>> getSkuSaleAttrAndValue(@PathVariable("skuId")Long skuId){
        List<SpuSaleAttr> spuSaleAttr = skuInfoService.getSkuSaleAttrAndValue(skuId);
         return Result.ok(spuSaleAttr);
    }

    /**
     * 查询ValuesSkuJson数据
     * @param skuId
     * @return
     */
    @GetMapping("/getValuesSkuJson/{skuId}")
    public Result<Map<String,String>> getValuesSkuJson(@PathVariable("skuId")Long skuId){
        Map<String,String> valuesSkuJson = skuInfoService.getValuesSkuJson(skuId);
        return Result.ok(valuesSkuJson);
    }
}
